package game.client;

import game.client.SwingGameWorld.GUI;
import game.client.jme.GameWorld;
import game.client.net.ConnectionListener;
import game.client.net.GameConnection;
import game.client.net.PlayerPositionSender;
import game.client.net.WorldObjectListener;
import game.common.net.GameHandler;
import game.common.util.Logger;
import game.common.world.Vector;

import java.io.IOException;
import java.util.Random;

import com.jme3.app.SimpleBulletApplication;

import sun.java2d.pipe.LoopPipe;

public class ClientBoot extends GameHandler implements ConnectionListener {	
	/* ---------------- PARAM SYNTAX ---------------- */
	public static final String PARAM_HOST = "-h";
	public static final String PARAM_PORT = "-p";
	
	/* --------------- DEFAULT VALUES --------------- */
	public static final String DEFAULT_HOST = "192.168.1.6";
	public static final int DEFAULT_PORT = 5353;
	
	/* --------------- PARAMETRIZABLE --------------- */
	private String host;
	private int port;
	
	/* ------------------ UTILITY ------------------- */
	private Logger log = Logger.getLogger(getClass());
	
	/* -------------- CLIENT COMPONENTS ------------- */
	private GameConnection connection;
	private GameWorld world;
	
	/* ---------------- SERVER DATA ----------------- */
	private String playerName;
	private Vector playerPosition;
	
	/* -------------------- IMPL -------------------- */
	public ClientBoot(String[] args) {
		
		// set default values
		host = DEFAULT_HOST;
		port = DEFAULT_PORT;
		
		readArgs(args);
	}

	private void readArgs(String[] args){
		int offset = 0; 
		
		while (offset < args.length){
			if (args[offset].equals(PARAM_HOST)){
				host = args[offset + 1];
				offset += 2;
			} else if (args[offset].equals(PARAM_PORT)){
				port = Integer.parseInt(args[offset + 1]);
				offset += 2;
			} else {
				log.debug("unknown param '" + args[offset] + "'");
				++offset;
			}
		}
	}
	
	public synchronized void init(){
		try {
			log.debug("opening connection " + host + ":" + port);
			this.world = new GameWorld();
			
			this.connection = new GameConnection(host, 
												 port, 
												 this, 
												 this.world);
			
			// waits for test first reply
			wait();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void connected(String playerName, Vector playerVector) {		
		log.debug("connected as " + playerName + " at location " + playerVector);
		
		this.playerName = playerName;
		this.playerPosition = playerVector;
		
		// wake up main thread
		this.notify();
	}
	
	public void start(){
	
		if(playerName == null)
			System.out.println("connection failed");
		else {
			while (true){
				try {
					Thread.sleep(Long.MAX_VALUE);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		ClientBoot boot = new ClientBoot(args);
		boot.init();
		boot.start();
	}
}
